package net.cruciblesoftware.ghostspacekillah;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.Random;

class SceneBackground {
    public static final int TYPE_BACKGROUND = Types.BACKGROUND;

    // first index is depth level, second is star positions
    private float stars[][];
    private Paint colors[];

    private Bitmap bgImg;
    private Rect src, dest;
    private float maxVelBg;
    private float bgScale;

    private Random rand;
    private int nStars, nLevels, maxVelStars;

    public SceneBackground() {
        logi("creating SceneBackground...");

        // figure out star configuration from settings
        nStars = Settings.getInt("num_of_stars");
        nLevels = Settings.getInt("num_of_star_levels");
        maxVelStars = Settings.getInt("max_vel_stars");
        logi("loaded star data:");
        logi("\tnStars: " + nStars);
        logi("\tnLevels: " + nLevels);
        logi("\tmaxVelStars: " + maxVelStars);

        // init the stars and colors
        rand = new Random();
        colors = new Paint[nLevels];
        stars = new float[nLevels][(nStars / nLevels) * 2];
        for(int i = 0; i < nLevels; i++) {
            for(int j = 0; j < stars[i].length; j += 2) {
                stars[i][j] = rand.nextInt(SceneView.width);
                stars[i][j+1] = rand.nextInt(SceneView.height);
            }
            colors[i] = new Paint();
            int component = (int)(128.0f * ((i+1.0f) / nLevels)) + 128;
            colors[i].setARGB(255, component, component, component);        
            logi("\tsetting star level " + i + " to color (" + component +
                 "," + component + "," + component + ")");
        }
        logi("\tfinished creating stars");

        // load background image, etc
        bgImg = Assets.getBitmap(TYPE_BACKGROUND);
        bgScale = (float)SceneView.height / (float)bgImg.getHeight();
        src = new Rect((int)(-SceneView.width / bgScale), 0, 
                       0, bgImg.getHeight());
        dest = new Rect(0, 0, SceneView.width, SceneView.height);
        maxVelBg = Settings.getPercent("max_vel_background");
        logi("\tmax_vel_background=" + maxVelBg);
    }


    public void update(float elapsed) {
        // update background position
        src.left += elapsed * (maxVelBg * SceneView.width);
        if(src.left > bgImg.getWidth())
            src.left = (int)(-SceneView.width / bgScale);
        src.right = src.left + (int)(SceneView.width / bgScale);

        // and the star positions
        for(int i = 0; i < nLevels; i++) {
            for(int j = 0; j < stars[i].length; j += 2) {
                stars[i][j+1] += maxVelStars * ((i+1.0f) / nLevels);
                if(stars[i][j+1] > SceneView.height) {
                    stars[i][j] = rand.nextInt(SceneView.width);
                    stars[i][j+1] = 0;
                }
            }
        }
    }


    public void draw(Canvas c) {
        // paint it black
        c.drawColor(Color.BLACK);

        // add some sparkling stars
        for(int i = 0; i < nLevels; i++)
            c.drawPoints(stars[i], colors[i]);

        // with an astrological topping
        c.drawBitmap(bgImg, src, dest, null);
    }


    /* Logging functions */
    private void logi(String msg) {
        android.util.Log.i(this.getClass().getName(), msg);
    }
    private void logw(String msg) {
        android.util.Log.w(this.getClass().getName(), msg);
    }
    private void loge(String msg) {
        android.util.Log.e(this.getClass().getName(), msg);
    }
}
